$(function () {
    // 默认加载部门树
    vm.getDept();
    // 初始化表格
    initTable();
});
// 初始化table
function initTable() {
    $('#alarmStat').bootstrapTable('destroy').bootstrapTable({
        url:'../device/stat/devAlarmTimeAndRate',
        method: 'post',
        queryParams: function(){
            var param = {};
            if(vm.q.curDeptId){
                param.curDeptId = vm.q.curDeptId;
            }
            if(vm.q.topNum){
                param.topNum = vm.q.topNum;
            }
            if(vm.q.beginTime){
                param.beginTime = gmt2DateStr(vm.q.beginTime);
            }
            if(vm.q.endTime){
                param.endTime =  gmt2DateStr(vm.q.endTime);
            }
            return param;
        },
        columns: [
            {
                field: 'stationName',
                title: '工序名称',
                align: 'center'
            },{
                field: 'devName',
                title: '设备名称',
                align: 'center'
            }, {
                field: 'devCode',
                title: '设备编号',
                align: 'center'
            }, {
                field: 'faName',
                title: '供应厂商',
                align: 'center'
            }, {
                field: 'alarmDuration',
                title: '报警时长(分钟)',
                align: 'center',
                cellStyle:cellStyle
            }],
        onExpandRow: function (index, row, $detail) {
            // 初始化子TABLE
            initSubTable(index, row, $detail);
        }
    })
}
initSubTable = function (index, row, $detail) {
    var cur_table = $detail.html('<table></table>').find('table');
    $(cur_table).bootstrapTable({
        data: row.alarmItems,
        columns: [{
            field: 'alarmCode',
            title: '报警码',
            align: 'center',
            width: '120px'
        },{
            field: 'alarmMsg',
            title: '报警内容',
            align: 'center'
        }, {
            field: 'times',
            title: '频次',
            align: 'center',
            width: '80px'
        }, {
            field: 'alarmDuration',
            title: '累计时长',
            align: 'center',
            width: '120px'
        }, {
            field: 'alarmId',
            visible: false
        }, {
            field: 'devId',
            visible: false
        }],
        onClickRow:function(row,$element){
            // 查询设备某种报警的报警明细信息
            openWindow({
                type: 2,
                title: '<i class="fa fa-bars"></i> 设备报警明细',
                content: '../analyse/devalarmrecord.html?devId=' + row.devId + '&alarmCode=' + row.alarmCode
            })
        }
    });
}
function cellStyle(value, row, index) {
    // 如果报警时长值超过阈值，那么标红
    return {
        css: {
            color: (value >= row.thresholdValue?'red':'green')
        }
    }
}

var ztree;
//部门树
var deptSetting = {
    data: {
        simpleData: {
            enable: true,
            idKey: "deptId",
            pIdKey: "parentId",
            rootPId: '01',
        },
        key: {
            url: "nourl",
            name: "complexName" //name 与 typeName 绑定
        },
    },
    callback: {
        beforeCheck: true,
        beforeClick: outTreeCheck
    }
};
function outTreeCheck(e, treeObj, treeNode) {
    //此处treeNode 为当前节点
    vm.q.curDeptId = treeObj.deptId;
    vm.search();
}

var vm = new Vue({
    el: '#rrapp',
    data: {
        q:{
            curDeptId: '01', // 所属车间地点ID
            topNum: 10, // 参数名称
            beginTime: '', // 开始时间
            endTime:'' // 结束时间
        },
        alarmDevList: [], // 报警设备列表
    },
    methods: {
        getDept: function () {
            //加载部门树
            Ajax.request({
                url: '../sys/user/treeDeptBySCX',
                async: false,
                successCallback: function (r) {
                    ztree = $.fn.zTree.init($("#deptTree"), deptSetting, r.list);
                    ztree.expandAll(true);
                    var node = ztree.getNodeByParam("deptId", '01');
                    if (node != null) {
                        vm.q.curDeptId = node.deptId;
                        ztree.selectNode(node);
                        ztree.expandNode(node)
                    }
                }
            });
        },
        search: function () {
            initTable();
        },
        reset: function(){
            vm.q = {
                curDeptId: '01',
                topNum: 10,
                beginTime: '',
                endTime:''
            };
            initTable();
        }
    }
});
